package com.mzy.linear_struct.hot100.哈希表;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: mizy3
 * @Date: 2024/08/08/下午11:02
 * @Description:
 */
public class Pro128最长连续序列 {

    private int longestConsecutive(int[] nums) {
        Map<Integer, Integer> map = new HashMap<>();
        // 输入：nums = [100,4,200,1,3,2]
        // 输出：4
        // 解释：最长数字连续序列是 [1, 2, 3, 4]。它的长度为 4。

        for (int num : nums) {
            map.put(num, num);
        }

        int ans = 0;
        for (int i = 0; i < nums.length; i++) {
            if(!map.containsKey(nums[i] - 1)){
                int x = nums[i] + 1;
                while (map.containsKey(x)) x++;
                ans = Math.max(ans, x - nums[i]);
            }
        }
        return ans;
    }
    public static void main(String[] args) {
        System.out.println(new Pro128最长连续序列().longestConsecutive(new int[]{100, 4, 200, 1, 3, 2}));
    }

}

























